package com.example.dell.intelligentagriculture;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.example.dell.intelligentagriculture.Service.WebService;
import com.example.dell.intelligentagriculture.fragment.HelpFragment;
import com.example.dell.intelligentagriculture.fragment.HomeFragment;
import com.example.dell.intelligentagriculture.fragment.SetFragment;
import com.example.dell.intelligentagriculture.utils.HttpUtil;
import com.example.dell.intelligentagriculture.utils.MyApp;
import com.example.dell.intelligentagriculture.utils.MyFvAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

    private TextView tvTitle;
    private ViewPager mViewPager;
    private RadioGroup rgMenu;
    private RadioButton rbHome, rbSet, rbHelp;
    private FragmentManager manager;
    private MyFvAdapter adapter;
    private List<Fragment> fragList;
    private String[] titleArr = {"智能农业", "设置", "帮助"};

    public MyApp myApp;
    private HttpUtil httpUtil;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    private void initView() {
        myApp = (MyApp) getApplication();


        manager = getSupportFragmentManager();
        tvTitle = findViewById(R.id.tv_title);
        mViewPager = findViewById(R.id.my_viewPager);
        rgMenu = findViewById(R.id.rg_menu);
        rbHome = findViewById(R.id.rb_home);
        rbSet = findViewById(R.id.rb_set);
        rbHelp = findViewById(R.id.rb_help);
        initFragList();
        adapter = new MyFvAdapter(manager, fragList);
        mViewPager.setAdapter(adapter);
        mViewPager.addOnPageChangeListener(this);
        rgMenu.setOnCheckedChangeListener(this);

        getDataFromServer();
        //开启服务
        Intent intent = new Intent(this, WebService.class);
        intent.putExtra("myapp", myApp);
        startService(intent);

    }


    /**
     * 从服务器中获取数据当前的最大值和最小值
     */
    private void getDataFromServer() {
        sp=getSharedPreferences("maxInfo", Context.MODE_PRIVATE);
        new Thread() {
            @Override
            public void run() {
                try {
                    httpUtil = new HttpUtil(sp);
                    httpUtil.getConfig(myApp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();


    }


    /**
     * 初始化fragment数据集合
     */
    private void initFragList() {
        fragList = new ArrayList<>();
        fragList.add(new HomeFragment());
        fragList.add(new SetFragment());
        fragList.add(new HelpFragment());
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //改变标题文字
        tvTitle.setText(titleArr[position]);
        switch (position) {
            case 0:
                rbHome.setChecked(true);
                break;
            case 1:
                rbSet.setChecked(true);
                break;
            case 2:
                rbHelp.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.rb_set:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.rb_help:
                mViewPager.setCurrentItem(2);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //调用方法，点击两次退出
            exitSystem();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 点击两次返回键退出程序
     */
    private long exittime = 0;

    private void exitSystem() {
        /**
         *  第一次    系统时间 50000  -  exittime[0] > 2000    exittime=50000  ---> 第一次点击的时间
         *  第二次    系统时间 650000 - 50000  =600000   exittime=650000;
         */
        if (System.currentTimeMillis() - exittime > 2000) {
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            exittime = System.currentTimeMillis();

        } else {

            finish();
            System.exit(0);
        }
    }
}